﻿using RW.Data.Graph;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RW.Data.Objects
{
    public class State : Dictionary<string, bool>
    {
        public State()
        {
            this.IsValid = true;
            this.Loops = new List<MultiGraphEdge>();
        }

        public State(IDictionary<string, bool> dictionary)
            : base(dictionary)
        {
            this.IsValid = true;
            this.Loops = new List<MultiGraphEdge>();
        }

        public string Text
        {
            get { return String.Join(",", this.Keys.Select(k => String.Format("{0}{1}", this[k] ? "" : "~", k[0]))); }
        }

        public bool IsValid { get; set; }
        public bool IsInitial { get; set; }

        public List<MultiGraphEdge> Loops { get; set; }

        public string LoopText
        {
            get { return String.Join(",", this.Loops.Select(l => String.Format("{0}({1})", l.ActionName, l.ActionCost))); }
        }

        public State Clone()
        {
            return new State(this);
        }

        public override bool Equals(object obj)
        {
            IDictionary<string, bool> dic = obj as IDictionary<string, bool>;

            if (dic == null)
                return false;

            return this.Keys.All(k => dic.ContainsKey(k) && this[k] == dic[k]);
        }

        public bool SatisfiesConditions(IDictionary<string, bool> conditions)
        {
            foreach (var condition in conditions)
            {
                if (this[condition.Key] != condition.Value)
                    return false;
            }
            return true;
        }
    }
}
